package string.homework;

/**
 * 字符串常量池
 */
public class StringDemo {
    public static void main(String[] args) {
        String s1 = "123abc";
        String s2 = "123abc";
        String s3 = "123abc";
        System.out.println(s1==s2);//true,地址相同
        System.out.println(s1==s3);//true
        String s4 = new String("123abc");

        System.out.println(s4==s1);//false

        s1 = s1+"!";//字符串是不变对象，修改内容会创建新的对象（地址改变）
        System.out.println(s1==s2);//false,地址变了
        String s="123";
        String s5 = s+"abc";//编译器不做操作，拼接字符串会产生新对象
        System.out.println(s5==s2);//false

        String s6 = "123"+"abc";//触发预计算特性，直接编译为String s5 = "123abc"会复用该u对象
        System.out.println(s6==s2);//true

        String s7 = "123abc";
        String s8 = new String("123abc");
        System.out.println(s7==s8);//false
        System.out.println(s7.equals(s8));
    }
}
